# -*- coding: utf-8 -*-

from twisted.web import server, resource
from twisted.internet import reactor
import simplejson as json


class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        data = request.args
        print data
        return "Good"
        return "<form action='survey' method='post'><input name='hihi' type='text'></form>"
        
        
    def render_POST(self, request):
        #print dir(request)
        
        print request.args

        print request.args['content']
        return "ok"
        
        data = request.args
        data = json.load( request.content )
        print data

        #print data
        return "Good"
        
        
        # servicename
        # arguments
        try:
            data["servicename"]
        except Exception, e:
            print e
        else:
            print "coucou"
            #try:
                #if
        return data["hihi"][0]       
        

site = server.Site(Simple())
reactor.(12345, site)
reactor.run()


#import BaseHTTPServer

#class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    #def do_GET(self):
        #if self.path == '/survey':
            #self.ok()
            #self.send_header('Content-type', 'text/html')
            #self.end_headers()
            #self.wfile.write("<form action='survey' method='post'><input type='text'></form>")
            #self.wfile.flush()
            ##self.do_something()
        #else: 
            #self.error()
            
    #def do_POST(self):
        #print dir(self)
        #print self.rfile()
        #if self.path == '/survey':
            #try:
                ## Get arguments by reading body of request.
                ## We read this in chunks to avoid straining
                ## socket.read(); around the 10 or 15Mb mark, some platforms
                ## begin to have problems (bug #792570).
                #max_chunk_size = 10*1024*1024
                #size_remaining = int(self.headers["content-length"])
                #L = []
                #while size_remaining:
                    #chunk_size = min(size_remaining, max_chunk_size)
                    #L.append(self.rfile.read(chunk_size))
                    #size_remaining -= len(L[-1])
                #data = ''.join(L)
                #print data
                #self.ok()
            #except Exception, e: # This should only happen if the module is buggy
                ## internal error, report as HTTP server error
                #self.send_response(500)
        #else: 
            #self.error()            
            
    
    #def ok(self):
        #self.send_response(200)
        
        
    #def error(self):
        #self.send_error(404)
        
        
    #def do_something(self):
        #self.wfile.write("<p>Coucou</p>")
        #print 'hello world'
        
#server = BaseHTTPServer.HTTPServer(('',12345), WebRequestHandler)
#server.serve_forever()